﻿using AutoDeployForSims.Common;
using AutoDeployForSims.Entity;
using AutoDeployForSims.From18To10Cfg;
using AutoDeployForSims.Service;
using AutoDeployForSims.Utility;
using MyUtility.Utility;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AutoDeployForSims.ViewModel
{
    public class CFGAnalysisControlViewModel:ViewModelBase
    {
        private GetConfigService getConfigService = SingletonProvider<GetConfigService>.Instance;


        #region ViewModel
        SimuAppInfo CommInfo = new SimuAppInfo();
        List<SimuAppInfo> simuAppInfos = new List<SimuAppInfo>();
        

        private string cfgPath;
        public string CFGPath
        {
            get { return cfgPath; }
            set { cfgPath = value; RaisePropertyChanged("CFGPath"); }
        }

        private CfgType cfgAnalysisMode = CfgType.CSM_TD_10;

        public CfgType CfgAnalysisMode
        {
            get { return cfgAnalysisMode; }
            set
            { 
                cfgAnalysisMode = value; 
                RaisePropertyChanged("CfgAnalysisMode");
            }
        }
        

        #endregion



        public void GetCFGPath()
        {
            FolderBrowserDialog openFolderDialog = new FolderBrowserDialog();
            openFolderDialog.Description = "选择配置库文件夹";

            if (openFolderDialog.ShowDialog() == DialogResult.OK)
            {
                CFGPath = openFolderDialog.SelectedPath;
                GlobalVari.CFGPath = openFolderDialog.SelectedPath;
            }
        }

        public bool StartCFGAnalysis()
        {            
            bool isRight = false;
            //判断配置是否为当前选择版本
            if (CfgAnalysisMode != getConfigService.ReadConfig())
            {
                MessageBox.Show("请重新确定该配置是10版还是20版配置库！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return isRight;
            }
            //如果是20版配置库，则先转成10配置库
            if (CfgAnalysisMode == CfgType.CSM_TD_20)
            {
                DialogResult result = MessageBox.Show("是否需要生成10配置库？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
                if (result == DialogResult.Yes)
                {
                    bool isOk = false;
                    string strInfo = "";
                    string telCode18 = Path.GetFileName(GlobalVari.CFGPath);
                    if (telCode18 == "" || telCode18 == "null")
                    {
                        MessageBox.Show("电报码不正确！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return isOk;
                    }
                    try
                    {
                        Write10CfgFrom18 write10CfgFrom18 = new Write10CfgFrom18();
                        isOk = write10CfgFrom18.write18CFGService(telCode18);
                        strInfo = "转换analog";
                        if (isOk)
                        {
                            strInfo = strInfo + "成功！";
                        }
                        else
                        {
                            strInfo = strInfo + "失败！";
                        }
                        //写log

                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                
            }

            SelectSimuToolWindowViewModel SelectSimuToolWindowVM = SingletonProvider<SelectSimuToolWindowViewModel>.Instance;
            SelectSimuToolWindowVM.SimuToolInfoList.Clear();

            //分析有哪些接口，并弹窗提示没有的接口  
            if (getConfigService.ReadCBIConfig(ref CommInfo))
            {                
                //添加联锁模拟工具到列表
                SimuToolInfoEntity simuToolInfoEntity = new SimuToolInfoEntity(SelectSimuToolWindowVM.SimuToolInfoList.Count + 1, "联锁模拟", CommInfo.SimuAPPName, CommInfo.SimuAPPPath, true, false);                
                SelectSimuToolWindowVM.SimuToolInfoList.Add(simuToolInfoEntity);

                isRight = true;
            }
            else
            {
                MessageBox.Show("该站没有联锁接口或者该接口自动部署失败，请手动查看！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            if (getConfigService.ReadDYPConfig(ref simuAppInfos))
            {
                //添加电源屏模拟工具到列表
                int i = 0;
                foreach (var simuAppInfo in simuAppInfos)
                {
                    i++;
                    SimuToolInfoEntity simuToolInfoEntity = new SimuToolInfoEntity(SelectSimuToolWindowVM.SimuToolInfoList.Count + 1, "电源屏模拟" + i.ToString(), simuAppInfo.SimuAPPName, simuAppInfo.SimuAPPPath, true, false);
                    SelectSimuToolWindowVM.SimuToolInfoList.Add(simuToolInfoEntity);
                }
                isRight = true;
            }
            else
            {
                MessageBox.Show("该站没有电源屏接口或者该接口自动部署失败，请手动查看！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            if (getConfigService.ReadZPWConfig(ref CommInfo))
            {
                //添加ZPW2000模拟工具到列表
                SimuToolInfoEntity simuToolInfoEntity = new SimuToolInfoEntity(SelectSimuToolWindowVM.SimuToolInfoList.Count + 1, "ZPW2000模拟", CommInfo.SimuAPPName, CommInfo.SimuAPPPath, true, false);
                SelectSimuToolWindowVM.SimuToolInfoList.Add(simuToolInfoEntity);

                isRight = true;
            }
            else
            {
                MessageBox.Show("该站没有ZPW2000接口或者该接口自动部署失败，请手动查看！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            return isRight;
        }

        

    }
}
